package com.lysj.bhp.activity.util;

import com.lysj.bhp.activity.base.ActivityContent;
import com.lysj.bhp.activity.domain.Activity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class Timer extends ActivityContent {


    /**
     * @author drj
     * @date 2018/05/17 20:27
     * @Description
     */
    @Scheduled(fixedDelay = 60 * 1000, initialDelay = 5000)
    public void activityIsOverdue() {
        Long time = System.currentTimeMillis();
        // 查询活动状态为1,未开始的活动
        List<Activity> activityList = activityRepository.findByStatusOrderByCreateDateDesc(1);
        for (Activity activity : activityList) {
            //若当前时间大于活动开始时间
            if (time > activity.getStartDateTime().getTime()) {
                // 更改活动状态为进行中
                activity.setStatus(2);
            }
        }
        activityRepository.save(activityList);
        // 查询活动状态为4,进行中的活动
        List<Activity> conductActivityList = activityRepository.findByStatusOrderByCreateDateDesc(2);
        for (Activity activity : conductActivityList) {
            //若当前时间大于活动结束时间
            if (time > activity.getEndDateTime().getTime()) {
                // 更改活动状态为活动结束
                activity.setStatus(3);
            }
        }
        activityRepository.save(conductActivityList);
    }
}
